package pos.dato;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import pos.dominio.Usuario;


public class JDBCUsuarioDAO implements IUsuarioDAO {
    private Connection conn;


    public JDBCUsuarioDAO() {
    	conn = ConnectionManager.getInstance().checkOut();
       
    }
   
	
    public boolean insert( Usuario usuario) {
    boolean res=false;
	PreparedStatement stmt = null;
    String sql = "INSERT INTO Usuarios ( usuarioID,password, nombre, apellidos, dni,email,direccion,ciudad) VALUES ( ?, ?, ?, ?, ?,?,?,?) ";
    try {
        stmt = conn.prepareStatement(sql);

        stmt.setString(1, usuario.getUsuarioID());
        stmt.setString(2, usuario.getPassword());
        stmt.setString(3, usuario.getNombre());
        stmt.setString(4, usuario.getApellidos());
        stmt.setString(5, usuario.getDni());
        stmt.setString(6, usuario.getEmail());
        stmt.setString(7, usuario.getDireccion());
        stmt.setString(8, usuario.getCiudad());
        stmt.executeUpdate();
    } catch (SQLException e) {
        System.out.println("Message: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("ErrorCode: " + e.getErrorCode());
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
                res=true;
            }
        } catch (SQLException e) {
        }
    }return res;
    }
    public boolean  update( String nombre,String apellidos,String email,String direccion,String ciudad,String id){
    	boolean res=false;
    	PreparedStatement stmt = null;
    	Connection conn = ConnectionManager.getInstance().checkOut();
    	try {
    		
    		String sql ="UPDATE Usuarios SET nombre='"+nombre+"',"+"apellidos='"+apellidos+"',"+"email='"+email+"',"+"direccion='"+direccion+"',"+"ciudad='"+ciudad+"'"+"  WHERE usuarioID='"+id+"'";
    		stmt = conn.prepareStatement(sql);
    		stmt.executeUpdate();
    	   } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
                	res=true;
            } catch (SQLException e) {
            }
        }
    	return res;
    }  
    public boolean delete(String usuarioID) {
        String sql = "DELETE FROM Usuarios WHERE (usuarioID = ?) ";
        PreparedStatement stmt = null;
        boolean res=false;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, usuarioID);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
                res=true;
            } catch (SQLException e) {
            }
        }
        return res;
    }
    
   
    

    public List<Usuario> selectAllUsuarios() {
        Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List<Usuario> searchResults = new LinkedList<Usuario>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Usuarios";
            stmt = conn.prepareStatement(sql); 
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Usuario temp = new Usuario();
                temp.setUsuarioID(result.getString("usuarioID"));
                temp.setPassword(result.getString("password"));
                temp.setNombre(result.getString("nombre"));
                temp.setApellidos(result.getString("apellidos"));
                temp.setDni(result.getString("dni")); 
                temp.setEmail(result.getString("email"));
                temp.setDireccion(result.getString("direccion"));
                temp.setCiudad(result.getString("ciudad"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
    }
    public boolean select(Connection conn,String usuarioID,String password) {
        String sql = "SELECT * FROM Usuarios WHERE (usuarioID = ? ,password=?) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        Usuario c = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, usuarioID);

            result = stmt.executeQuery();

            result.next();
            c = new Usuario();
            c.setUsuarioID(result.getString("usuarioID"));
            c.setPassword(result.getString("password"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        if(c==null){
        	return false;
        }else{
        	return true;
        }
   
}
    

	
}


